function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}

function loadData() {
	loadDataConfig.isLoading = true;
	loadDataConfig.lastLoadDataTime = new Date().getTime();

	// var projectId = GetQueryString('projectId');

	Ext.Ajax.request({
		url : baseURL + 'stkosutilowlentitytreeservlet',
		method : 'POST',
		params : {
			projectId : projectId
		},
		callback : function(options, success, response) {
			var data = null;

			if (success) {
				var json = Ext.util.JSON.decode(response.responseText);
				if (json.success) {
					data = JSON.stringify(json.result);
				} else {
					Ext.Msg.show({
						title : '提示',
						msg : json.msg,
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.ERROR
					});
				}
			} else {
				Ext.Msg.show({
					title : '提示',
					msg : 'Getting owlEntities tree error! ' + response.status
							+ ' ' + response.statusText,
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.ERROR
				});
			}

			loadDataConfig.isLoading = false;
			loadDataConfig.data = data;
		}
	});
}

/**
 * 创建绘图区域element
 */
function createDrawGraphEl() {
	var nEl = document.createElement('div');
	nEl.setAttribute('id', drawGraphElId);
	nEl
			.setAttribute('style',
					'display: block;margin: 0px 0px;padding: 0px 0px;overflow: auto;border: none;');

	// var iframeID = GetQueryString("iframeId");

	var w = 0, h = 0;

	var iframe = parent.document.getElementById(iframeId);
	if (iframe) {
		var parentNode = iframe.parentNode;
		if (parentNode) {
			w = parentNode.offsetWidth <= 0 ? 0 : parentNode.offsetWidth;
			h = parentNode.offsetHeight <= 0 ? 0 : parentNode.offsetHeight;
		}
	}

	nEl.style.width = w + "px";
	nEl.style.height = h + "px";

	document.body.appendChild(nEl);
}

/**
 * 创建图形显示类型区域element
 */
function createChoiceGraphTypeEl() {
	var nEl = document.createElement('div');
	nEl.setAttribute('id', choiceGraphTypeElId);
	nEl
			.setAttribute(
					'style',
					'margin: 2px 2px;padding: 6px 10px;border-radius:2px 2px;box-shadow: 0px 0px 5px #cca;background:none;border: none;display: inline-block;width: 140px;height: 18px;line-height: 18px;overflow: hidden;text-align: center;font-family: tahoma, arial, verdana, sans-serif;font-size: 12px;color: black;');

	var a_vt = document.createElement('a');
	a_vt.setAttribute('href', 'javascript:void(0);');
	a_vt.setAttribute('title', '树型');
	a_vt.style.textDecoration = 'none';
	a_vt.innerHTML = '树型';
	a_vt.onclick = function() {
		drawGraph('vt');
	};
	nEl.appendChild(a_vt);

	var a_ht = document.createElement('a');
	a_ht.setAttribute('href', 'javascript:void(0);');
	a_ht.setAttribute('title', '左树型');
	a_ht.style.textDecoration = 'none';
	a_ht.style.marginLeft = '6px';
	a_ht.innerHTML = '左树型';
	a_ht.onclick = function() {
		drawGraph('ht');
	};
	nEl.appendChild(a_ht);

	var a_ft = document.createElement('a');
	a_ft.setAttribute('href', 'javascript:void(0);');
	a_ft.setAttribute('title', '力导向型');
	a_ft.style.textDecoration = 'none';
	a_ft.style.marginLeft = '6px';
	a_ft.innerHTML = '力导向型';
	a_ft.onclick = function() {
		drawGraph('ft');
	};
	nEl.appendChild(a_ft);

	document.body.appendChild(nEl);

	StkosutilSuspension.init(choiceGraphTypeElId);
}

function drawGraph(type) {
	if (loadDataConfig.data == null) {
		loadData();
	} else if ((new Date().getTime() - loadDataConfig.lastLoadDataTime) > 10 * 60 * 1000) {
		loadData();
	}

	loop(type);
}

function loop(type) {
	if (loadDataConfig.isLoading) {
		setTimeout('loop("' + type + '")', 350);
	}

	if (loadDataConfig.data == null) {
		return;
	}

	var tree = null;

	switch (type) {
	case 'vt':
		tree = new D3jsVTree({
			elId : drawGraphElId,
			data : loadDataConfig.data
		});
		break;
	case 'ht':
		tree = new D3jsHTree({
			elId : drawGraphElId,
			data : loadDataConfig.data
		});
		break;
	case 'ft':
		tree = new D3jsFTree({
			elId : drawGraphElId,
			data : loadDataConfig.data
		});
		break;
	default:
		break;
	}

	if (tree) {
		tree.draw();
	}
}
